//
//  LPCostomLabelAndTextFieldAndImageView.m
//  ShangHaiHuDongApp
//
//  Created by 刘应平 on 2017/6/13.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "LPCostomLabelAndTextFieldAndImageView.h"

@interface LPCostomLabelAndTextFieldAndImageView ()<UITextFieldDelegate>


/**  右侧图片  */
@property (strong, nonatomic) UIImageView  *rightImageView;
/**  左侧提示图  */
@property (strong, nonatomic) UIImageView  *tishiImageView;

@end
@implementation LPCostomLabelAndTextFieldAndImageView

- (instancetype)initWithFrame:(CGRect)frame title:(NSString *)title placehold:(NSString *)placehold imageString:(NSString *)imageString
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.tishiImageView];
        self.tishiImageView.hidden = YES;
        [self addSubview:self.firstLabel];
        [self addSubview:self.secondTextField];
        [self addSubview:self.rightImageView];
        self.firstLabel.text = title;
        self.secondTextField.placeholder = placehold;
        self.rightImageView.image = [UIImage imageNamed:imageString];
        
        self.firstLabel.sd_layout
        .leftSpaceToView(self, 12 * [UIScreen zj_screenWScale])
        .topEqualToView(self)
        .widthIs([UIScreen zj_screenWith] - 24 * [UIScreen zj_screenWScale] )
        .heightIs(20 * [UIScreen zj_screenHScale]);
        
        
        self.secondTextField.sd_layout
        .leftSpaceToView(self, 12 * [UIScreen zj_screenWScale])
        .topSpaceToView(self.firstLabel, 10 * [UIScreen zj_screenHScale])
        .widthIs([UIScreen zj_screenWith] - 24 * [UIScreen zj_screenWScale])
        .heightIs(44 * [UIScreen zj_screenHScale]);
        
        self.rightImageView.sd_layout
        .rightSpaceToView(self, 12 * [UIScreen zj_screenWScale])
        .topSpaceToView(self.firstLabel, 10* [UIScreen zj_screenHScale])
        .widthIs(44 * [UIScreen zj_screenHScale])
        .heightIs(44 * [UIScreen zj_screenHScale]);
        if (imageString.length != 0) {
            [self.secondTextField setTextFieldRightforWidth:44 * [UIScreen zj_screenHScale]];
        }else{
            [self.secondTextField setTextFieldRightforWidth:12 * [UIScreen zj_screenWScale]];
        }
        
        
    }
    return self;
}
- (void)setIsShow:(BOOL)isShow{
    _isShow = isShow;
    if (isShow == YES) {
        self.tishiImageView.hidden = NO;
        self.tishiImageView.sd_layout
        .leftSpaceToView(self, 12 * [UIScreen zj_screenWScale])
        .topSpaceToView(self, 6 * [UIScreen zj_screenHScale])
        .widthIs(8 * [UIScreen zj_screenHScale])
        .heightIs(8 * [UIScreen zj_screenHScale]);
        
        self.firstLabel.sd_layout.leftSpaceToView(self.tishiImageView, 5 * [UIScreen zj_screenWScale]);
        [self.firstLabel updateLayout];
        
    }
}

- (void)setSpace:(CGFloat)space{
    _space = space;
    self.firstLabel.sd_layout.leftSpaceToView(self, space);
    [self.firstLabel updateLayout];
    
    self.secondTextField.sd_layout.leftSpaceToView(self, space)
    .widthIs([UIScreen zj_screenWith] - space * 2);
    [self.secondTextField updateLayout];
}
- (void)tapImage{
    if (self.delegate) {
        [self.delegate LPCostomLabelAndTextFieldAndImageWith:self click:YES];
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    if (self.maxNumber != 0 && toBeString.length >= self.maxNumber) {
        textField.text = [toBeString substringToIndex:self.maxNumber];
        return NO;
    }
    return YES;

}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    [textField resignFirstResponder];//取消第一响应者
    
    return YES;
}

- (UILabel *)firstLabel{
    if (!_firstLabel) {
        _firstLabel = ({
            UILabel *label = [UILabel new];
            label.textAlignment = NSTextAlignmentLeft;
            label.textColor = [UIColor textBlackColor];
            label.backgroundColor = [UIColor clearColor];
            label.font = LYPFONT(15);
            label;
        });
    }
    return _firstLabel;
}


- (UITextField *)secondTextField{
    if (!_secondTextField) {
        _secondTextField = ({
            UITextField *textField =  [UITextField new];
            textField.font = [UIFont systemFontOfSize:15 * [UIScreen zj_textFontScale]];
            textField.textColor = [UIColor textBlackColor];
            [textField setTextFieldLeftforWidth:12 * [UIScreen zj_screenWScale]];
            textField.textAlignment = NSTextAlignmentLeft;
            textField.userInteractionEnabled = NO;
            textField.layer.borderWidth = 1;
            textField.layer.borderColor = [UIColor zj_colorWithHexString:@"ebebeb"].CGColor;
            textField.layer.cornerRadius = 3;
            textField.layer.masksToBounds = YES;
            textField.returnKeyType = UIKeyboardTypeTwitter;
            textField.delegate = self;
            textField;
        });
        
    }
    return _secondTextField;
    
}

- (UIImageView *)rightImageView{
    if (!_rightImageView) {
        _rightImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 44 * [UIScreen zj_screenHScale], 44 * [UIScreen zj_screenHScale])];
        _rightImageView.contentMode = UIViewContentModeCenter;
        _rightImageView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImage)];
        [_rightImageView addGestureRecognizer:tap];
    }
    return _rightImageView;
}

- (UIImageView *)tishiImageView{
    if (!_tishiImageView) {
        _tishiImageView = [UIImageView new];
        _tishiImageView.contentMode = UIViewContentModeCenter;
        _tishiImageView.image = [UIImage imageNamed:@"tishiL"];
    }
    return _tishiImageView;
}

@end
